from google.appengine.ext import db
from google.appengine.api import memcache

class CDN(db.Model):
    id = db.StringProperty()
    url = db.StringProperty()

class Framework(db.Model):
    id = db.StringProperty()
    url = db.StringProperty()
    
class Country(db.Model):
    code2 = db.StringProperty()
    code3 = db.StringProperty()
    name = db.StringProperty()

class Browser(db.Model):
    name = db.StringProperty()

class Placement(db.Model):
    cdn = db.ReferenceProperty(CDN)
    framework = db.ReferenceProperty(Framework)
    url = db.StringProperty()

    @staticmethod
    def get_table():
        result = {}

        for framework in Framework.all():
            frm = (framework.id, framework.url)
            result[frm] = {}
            for cdn in CDN.all():
                result[frm][(cdn.id, cdn.url)] = None
            
        for placement in Placement.all():
            result[(placement.framework.id, placement.framework.url)][(placement.cdn.id, placement.cdn.url)] = placement.key().id()

        return result

class Score(db.Model):
    placement = db.ReferenceProperty(Placement)
    country = db.ReferenceProperty(Country)
    browser = db.ReferenceProperty(Browser)
    version = db.StringProperty
    uid = db.DateTimeProperty
    score = db.IntegerProperty